package com.fc.mall.decoration.page.service;

import com.fc.mall.decoration.page.dao.CustomPageRepository;
import com.fc.mall.decoration.page.exception.CustomPageNotFoundException;
import com.fc.mall.decoration.page.model.CustomPage;
import com.fc.mall.decoration.page.object.CustomPageCreate;
import com.fc.mall.decoration.page.object.CustomPageUpdate;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class CustomPageService {
    @Value("${app.page.main}")
    String mainPageId;

    @Autowired
    private CustomPageRepository customCustomPageRepository;

    @Autowired
    private MapperFacade mapperFacade;

    public CustomPage createCustomPage(CustomPageCreate customPageCreate) {
        CustomPage customPage = CustomPage.createNew();
        mapperFacade.map(customPageCreate, customPage);
        CustomPage createdPage = customCustomPageRepository.save(customPage);
        return createdPage;
    }

    public CustomPage updateCustomPage(String pageId, CustomPageUpdate customPageUpdate) {
        CustomPage customPage = getCustomPage(pageId, false,null);
        mapperFacade.map(customPageUpdate, customPage);
        customPage.setUpdatedAt(new DateTime());
        CustomPage updatedPage = customCustomPageRepository.save(customPage);
        return updatedPage;
    }

    public CustomPage updateCustomPage(CustomPage customPageUpdate) {
        customPageUpdate.setUpdatedAt(new DateTime());
        CustomPage updatedPage = customCustomPageRepository.save(customPageUpdate);
        return updatedPage;
    }

    public void removeDefaultPages(String except) {
        List<CustomPage> defaultPages = customCustomPageRepository.getDefaultPages();
        for(CustomPage page: defaultPages){
            if(page.getId().equals(except)){
                continue;
            }
            page.setUpdatedAt(new DateTime());
            page.setIsDefault(false);
            customCustomPageRepository.save(page);
        }
    }

    public CustomPage getMainPage() {
        List<CustomPage> allActivePages = customCustomPageRepository.getAllActivePages();
        if(null != allActivePages && !allActivePages.isEmpty())
        {
            return allActivePages.get(0);
        }
        /**  判断时间是否已过期
        for(CustomPage page: allActivePages){ //在活动期内的页面
            DateTime now = new DateTime();
            if(page.getActiveTime() == null || page.getExpireTime() == null){
                continue;
            }
            if(page.getActiveTime().isBefore(now) && page.getExpireTime().isAfter(now)){
                return page;
            }
        }*/
        //设置为默认页的主页
        List<CustomPage> defaultPages = customCustomPageRepository.getDefaultPages();
        if( null != defaultPages && !defaultPages.isEmpty())
        {
            return defaultPages.get(0);
        }
        //系统配置的备选页
        return getCustomPage(mainPageId, true, true);

    }

    public Page<CustomPage> searchCustomPages(String keyword, boolean activeOnly, int page, int size) {
        Page<CustomPage> pages = customCustomPageRepository.searchCustomPages(keyword, activeOnly, page, size);
        return pages;
    }

    public Page<CustomPage> getCustomPages(boolean activeOnly, int page, int size) {
        Page<CustomPage> pages = customCustomPageRepository.getCustomPages(activeOnly, page, size);
        return pages;
    }

    public void deletePage(String pageId) {
        CustomPage customPage = getCustomPage(pageId, false, null);
        customCustomPageRepository.delete(customPage);
    }

    public CustomPage getCustomPage(String pageId, boolean activeOnly, Boolean isDefault) {
        CustomPage customPage = customCustomPageRepository.getCustomPage(pageId, activeOnly, isDefault);
        if (customPage == null) {
            throw new CustomPageNotFoundException(pageId);
        }
        return customPage;
    }

    public CustomPage getCustomPage(String pageId) {
        CustomPage customPage = customCustomPageRepository.getCustomPage(pageId);
        if (customPage == null) {
            throw new CustomPageNotFoundException(pageId);
        }
        return customPage;
    }
}
